import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MoveFrame extends JFrame{
	 private String[] TechnologyName = {"Infantry", "Automatic Weapons", "Rocket Launchers", "Tanks", "Improved Tanks", "Fighter Planes"};
	 private JComboBox[] number;
	 private JButton submit, cancel;
	 private JComboBox<String> destination; 
	 private Territory Source;
	 private MyHistoryPanel historyPanel;
	 private Map map;

	 public MoveFrame(MyHistoryPanel _historyPanel, Territory _Source){
		super("Create a new move order");
		Source = _Source;
		historyPanel = _historyPanel;
		
		setLayout(new GridLayout(9, 2, 10, 10));	   
		
	  	add(new JLabel("Move from " + Source.getLocation()));
	  	add(new JLabel("    "));
	  	
	  	number = new JComboBox[6];
	  	
	  	for (int i = 0; i < 6; i++){
	  	   add(new JLabel(TechnologyName[i]));
	  	   number[i] = new JComboBox();
	  	   number[i].addItem(0);
	  	   for (int j = 1; j <= Source.getTroopAmount(i); j++)
	  		   number[i].addItem(j);
	  	   add(number[i]);
	  	}
	  	
	  	add(new JLabel("To Destiantion: "));
	  	destination = new JComboBox<String>();
	  	destination.addItem("test area");
	  	for (Territory t : Source.getOwner().getTerritoryList())
	  	   destination.addItem(t.getLocation());
	  	
	  	add(destination);
	  	submit = new JButton("Submit");
	  	cancel = new JButton("Cancel");
	  	
	  	add(submit);
	  	add(cancel);
	  	submit.addActionListener(new submitListener(this));
	  	cancel.addActionListener(new cancelListener(this));
	  	
	  	setSize(600, 600);
	  	setVisible(true);
	 }
	 
	class submitListener implements ActionListener{
   
		JFrame frame;
	  public submitListener(JFrame _frame){
			frame = _frame;
		}
	  public void actionPerformed(ActionEvent arg0) {
		Territory Destination = Source.getOwner().getTerritoryList(destination.getSelectedIndex());
		int[] amount = new int[6];
		for (int i = 0; i < 6; i++)
		   amount[i] = (int)number[i].getSelectedItem();
	  	 Move temp = new Move(Source.getOwner(), Source, Destination, amount);
	  	 //if (temp.checkOrder(map))
	  	   historyPanel.addHistory(temp);
	  	 frame.dispose();
	}
	 }
	 
	class cancelListener implements ActionListener{
		JFrame frame;
		public cancelListener(JFrame _frame){
			frame = _frame;
		}
		public void actionPerformed(ActionEvent e) {
			frame.dispose();
		}
		
	}
}
